import requests
from pyquery import PyQuery as pq
import redis
def main():
    url = "https://book.douban.com/tag/?view=type&icn=index-sorttags-all"
    res = requests.get(url)
    #获取网页内容
    html = res.content.decode("utf-8")
    #解析网页数据
    doc = pq(html)
    #获取网页中所以图书标签的链接信息
    items = doc("table.tagCol tr td a")
    #print(items)

    #连接redis数据库
    link = redis.StrictRedis(host='127.0.0.1',port=6379,db=0)
    #遍历封装并且返回
    for a in items.items():
        #拼装tag的url的信息
        tag = a.attr.href
        #并将信息以tag：start_urls写入redis数据库中
        #print(tag)
        link.lpush("book:tag_urls",tag)
    print("一共写入tag_urls%d个"%(len(items)))

if __name__ == '__main__':
    main()